package com.bcbim.api_web.table_head.controller;

import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.operation_maintenance_interface.table_head.dao.SpaceTableHeadDAO;
import com.bcbim.operation_maintenance_interface.table_head.dto.TableCheckDTO;
import com.bcbim.operation_maintenance_interface.table_head.service.SpaceTableHeadService;
import com.bcbim.operation_maintenance_interface.table_head.vo.SpaceTableHeadVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V 1.0.0
 * @Description：空间信息表头设置controller
 * @Author: haomeijie
 * @Date: 2022/3/9 10:01
 **/
@RestController
@RequestMapping("/spaceTable")
public class SpaceTableHeadController {
    @Reference(protocol = "dubbo", group = "spaceTableHead", version = "1.0.0")
    private SpaceTableHeadService spaceTableHeadServiceImpl;

    /**
     * 空间信息表头设置列表
     *
     * @Author: haomeijie
     * @Date: 2022/3/9 10:13
     */
    @RequestMapping("/list")
    public ReturnMsg listSpaceTableHead(String projectId) {
        //判断用户是否设置过表头
        TableCheckDTO tableCheckDTO = new TableCheckDTO();
        tableCheckDTO.setUserId(AuthUtil.getUserIdDecrypt());
        tableCheckDTO.setProjectId(projectId);
        boolean exit = spaceTableHeadServiceImpl.checkSpaceTableHeadByUserId(tableCheckDTO);
        if (!exit) {
            //如果不存在，查询模板
            tableCheckDTO.setUserId(null);
            tableCheckDTO.setProjectId(null);
        }
        //查询用户设置过的
        List<SpaceTableHeadVO> list = spaceTableHeadServiceImpl.listSpaceTableHead(tableCheckDTO);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * 保存表头设置
     *
     * @param tableCheckDTO
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2022/3/9 16:38
     */
    @RequestMapping("/save")
    @Transactional
    public ReturnMsg saveSpaceTableHead(TableCheckDTO tableCheckDTO) {
        //判断用户是否设置过表头
        tableCheckDTO.setUserId(AuthUtil.getUserIdDecrypt());
        boolean exit = spaceTableHeadServiceImpl.checkSpaceTableHeadByUserId(tableCheckDTO);
        if (exit) {
            //进行设置修改
            List<SpaceTableHeadDAO> list = new ArrayList<>();
            for (String id : tableCheckDTO.getIds()) {
                SpaceTableHeadDAO spaceTableHeadDAO = new SpaceTableHeadDAO();
                spaceTableHeadDAO.setId(id)
                        .setCheckType(Constant.CHECK);
                list.add(spaceTableHeadDAO);
            }
            //修改勾选的
            int i = spaceTableHeadServiceImpl.batchUpdateSpaceTableHeadCheck(list);
            //取消未勾选的
            spaceTableHeadServiceImpl.batchNoChecked(tableCheckDTO);
            return i > 0 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
        }
        //没有设置过，获取模板
        List<SpaceTableHeadVO> list = spaceTableHeadServiceImpl.listSpaceTableHead(new TableCheckDTO());
        List<SpaceTableHeadDAO> headList = new ArrayList<>();
        for (SpaceTableHeadVO spaceTableHeadVO : list) {
            SpaceTableHeadDAO spaceTableHeadDAO = new SpaceTableHeadDAO();
            //复制属性
            BeanUtils.copyProperties(spaceTableHeadVO, spaceTableHeadDAO);
            spaceTableHeadDAO.setId(GuidUtil.get())
                    .setUserId(AuthUtil.getUserIdDecrypt())
                    .setProjectId(tableCheckDTO.getProjectId())
                    .setDel(Constant.NOT_DEL);
            //根据传值判断哪些勾选
            for (String id : tableCheckDTO.getIds()) {
                if (id.equals(spaceTableHeadVO.getId())) {
                    spaceTableHeadDAO.setCheckType(Constant.CHECK);
                }
            }
            headList.add(spaceTableHeadDAO);
        }
        int j = spaceTableHeadServiceImpl.batchInsertSpaceTableHead(headList);
        return j > 0 ? ReturnMsg.consSuccess(Constant.SAVE_SUCCESS) : ReturnMsg.consFail(Constant.SAVE_FAIL);
    }
}
